package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.domain.vo.TiXianVo;
import com.ruoyi.project.system.mapper.SysEarningsMapper;
import com.ruoyi.project.system.mapper.SysExtractMapper;
import com.ruoyi.project.system.mapper.SysQueryPayMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISysExtractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 提现Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class SysExtractServiceImpl implements ISysExtractService {


    @Autowired
    private SysExtractMapper sysExtractMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysQueryPayMapper sysQueryPayMapper;

    @Autowired
    private SysEarningsMapper sysEarningsMapper;

    @Override
    public TiXianVo queryTiXianList() {
        BigDecimal total = new BigDecimal(0);
        BigDecimal refundTotal = new BigDecimal(0);
        BigDecimal actualTotal;
        List<TiXian> tiXians = sysQueryPayMapper.tiXianList(SecurityUtils.getUserId());
        if (tiXians.isEmpty()) {
            return new TiXianVo();
        }
        for (TiXian tiXian : tiXians) {
            if (tiXian.getTypeId() > 0) {
                total = total.add(tiXian.getJe());
            } else {
                refundTotal = refundTotal.add(tiXian.getJe());
            }
        }
        actualTotal = total.subtract(refundTotal);
        return new TiXianVo(total, refundTotal, actualTotal, tiXians);
    }

    @Override
    @Transactional
    public String addTXLIst(TiXianVo tiXianVo) {
        String extractId = "TX" + IdUtils.fastSimpleUUID().substring(0, 8).toUpperCase();
        for (TiXian tiXian : tiXianVo.getTiXians()) {
            if (tiXian.getTypeId() == 1) {
                sysQueryPayMapper.updateSysQueryPay(new SysQueryPay(tiXian.getDh()).setBrokerageId(extractId));
            } else if (tiXian.getTypeId() == 2) {
                sysQueryPayMapper.updateSysQueryPay(new SysQueryPay(tiXian.getDh()).setRoyaltyId(extractId));
            } else if (tiXian.getTypeId() == 3 || tiXian.getTypeId() == 4) {
                sysEarningsMapper.updateSysEarnings(new SysEarnings(Long.parseLong(tiXian.getDh()), extractId));
            }

              else if (tiXian.getTypeId() == -1) {
                sysQueryPayMapper.updateSysQueryPay(new SysQueryPay(tiXian.getDh()).setRefundDis("2"));
            } else if (tiXian.getTypeId() == -2) {
                sysQueryPayMapper.updateSysQueryPay(new SysQueryPay(tiXian.getDh()).setRefundDis_("2"));
            } else if (tiXian.getTypeId() == -3) {
                sysEarningsMapper.updateSysEarnings(new SysEarnings(tiXian.getDh()).setRefundDis("2"));
            }
        }
        // 添加提现单
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        SysExtract sysExtract = new SysExtract(extractId, userId, sysUser.getAlipayName(),
                sysUser.getAlipayAccount(), tiXianVo.getActualTotal(), "审批中");
        boolean isTx = "2".equals(sysUser.getEnableTc()) && tiXianVo.getActualTotal().compareTo(sysUser.getMaxTc()) == -1;
        if (isTx) {                         // 自动转账
            sysExtract.setStatus("未转账");
            sysExtractMapper.insertSysExtract(sysExtract);
            // 启用异步转账
            AsyncManager.me().execute(AsyncFactory.zhuanZhang(sysExtract));
        } else {
            sysExtractMapper.insertSysExtract(sysExtract);
        }
        return extractId;
    }

    /**
     * 查询提现
     *
     * @param extractId 提现主键
     * @return 提现
     */
    @Override
    public SysExtract selectSysExtractByExtractId(String extractId) {
        return sysExtractMapper.selectSysExtractByExtractId(extractId);
    }

    /**
     * 查询提现列表
     *
     * @param sysExtract 提现
     * @return 提现
     */
    @Override
    public List<SysExtract> selectSysExtractList(SysExtract sysExtract) {
        return sysExtractMapper.selectSysExtractList(sysExtract);
    }

    /**
     * 新增提现
     *
     * @param sysExtract 提现
     * @return 结果
     */
    @Override
    public int insertSysExtract(SysExtract sysExtract) {
        sysExtract.setCreateTime(DateUtils.getNowDate());
        return sysExtractMapper.insertSysExtract(sysExtract);
    }

    /**
     * 修改提现
     *
     * @param sysExtract 提现
     * @return 结果
     */
    @Override
    public int updateSysExtract(SysExtract sysExtract) {
        sysExtract.setUpdateTime(DateUtils.getNowDate());
        return sysExtractMapper.updateSysExtract(sysExtract);
    }

}
